<?php
class Mama {
/**
* Create a new callback
*
* @param string $fun function to callback
* @param array $args parameters to be passed to the callback
* @return Closure
*/
public static function bild1($fun, $args = array())
{
// Outside get_called_class()
$called_class = get_called_class();
return function() use ($fun, $args, $called_class) {
return call_user_func_array(array($called_class, $fun), $args);
};
}
/**
* Create a new callback
*
* @param string $fun function to callback
* @param array $args parameters to be passed to the callback
* @return Closure
*/
public static function bild2($fun, $args = array())
{
return function() use ($fun, $args) {
// Inside get_called_class()
$called_class = get_called_class();
return call_user_func_array(array($called_class, $fun), $args);
};
}
/**
* Print something for demonstration
*
* @return void
*/
public static function print_something()
{
echo "I told'ya!" . PHP_EOL;
}
}
class Lama extends Mama {
/**
* Print something for demonstration
*
* @return void
*/
public static function print_something()
{
echo "Lama lo?" . PHP_EOL;
}
}
$bild1 = Lama::bild1("print_something");
$bild1(); // "Lama lo?"
$bild2 = Lama::bild2("print_something");
$bild2(); // "I told'ya!"
1